{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 气象大数据云平台的模式数据读取\n",
    "\n",
    "#### —— nmc_met_io程序库使用说明\n",
    "\n",
    "国家气象中心天气预报技术研发室    \n",
    "June, 2020    \n",
    "Kan Dai "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set up things\n",
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<xarray.core.options.set_options at 0x7fc33c112590>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import xarray as xr\n",
    "\n",
    "from nmc_met_io.retrieve_cmadaas import cmadaas_model_grid\n",
    "\n",
    "xr.set_options(display_style=\"text\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取单个数值模式预报数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.Dataset&gt;\n",
       "Dimensions:                  (lat: 1441, lon: 2880, time: 1)\n",
       "Coordinates:\n",
       "  * time                     (time) datetime64[ns] 2020-07-29\n",
       "  * lat                      (lat) float64 90.0 89.88 89.75 ... -89.88 -90.0\n",
       "  * lon                      (lon) float64 0.0 0.125 0.25 ... 359.6 359.8 359.9\n",
       "    forecast_reference_time  datetime64[ns] 2020-07-29\n",
       "    forecast_period          (time) float64 0.0\n",
       "Data variables:\n",
       "    temperature              (time, lat, lon) float32 1.706543 ... -50.730957\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    Origin:       CIMISS Server by MUSIC API</pre>"
      ],
      "text/plain": [
       "<xarray.Dataset>\n",
       "Dimensions:                  (lat: 1441, lon: 2880, time: 1)\n",
       "Coordinates:\n",
       "  * time                     (time) datetime64[ns] 2020-07-29\n",
       "  * lat                      (lat) float64 90.0 89.88 89.75 ... -89.88 -90.0\n",
       "  * lon                      (lon) float64 0.0 0.125 0.25 ... 359.6 359.8 359.9\n",
       "    forecast_reference_time  datetime64[ns] 2020-07-29\n",
       "    forecast_period          (time) float64 0.0\n",
       "Data variables:\n",
       "    temperature              (time, lat, lon) float32 1.706543 ... -50.730957\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    Origin:       CIMISS Server by MUSIC API"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set retrieve parameters\n",
    "dataCode = \"NAFP_ECMF_C1D_GLB_FOR\"    # 资料代码: 大气模式确定性预报产品\n",
    "init_time = \"2020072900\"              # 起报时间: \n",
    "fcst_Ele = \"TEM\"\n",
    "levelType = 1\n",
    "fcastLevel = 0\n",
    "validTime = 0\n",
    "\n",
    "# retrieve data from CMADaaS\n",
    "data = cmadaas_model_grid(dataCode, init_time, validTime, fcst_Ele, fcastLevel, levelType, \n",
    "                          varname='temperature', units='Degree', scale_off=[1.0, -273.15],\n",
    "                          levattrs={'long_name':'height_above_ground', 'units':'m', '_CoordinateAxisType':'Height'})\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取CRA40再分析数据\n",
    "\n",
    "CRA40为中国研制的第一套长时间序列全球大气再分析数据集-逐6小时产品，覆盖全球范围，时间跨度40年(197901-201812)并准实时更新，水平分辨率34公里、0.25°、0.5°、1.0°、2.5°。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>&lt;xarray.Dataset&gt;\n",
       "Dimensions:                  (lat: 361, level: 1, lon: 720, time: 1)\n",
       "Coordinates:\n",
       "  * time                     (time) datetime64[ns] 2020-04-19\n",
       "  * level                    (level) int64 500\n",
       "  * lat                      (lat) float64 90.0 89.5 89.0 ... -89.0 -89.5 -90.0\n",
       "  * lon                      (lon) float64 0.0 0.5 1.0 1.5 ... 358.5 359.0 359.5\n",
       "    forecast_reference_time  datetime64[ns] 2020-04-19\n",
       "    forecast_period          (time) float64 0.0\n",
       "Data variables:\n",
       "    geopotential_height      (time, level, lat, lon) float32 5.14e+03 ... 4.8...\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    Origin:       CIMISS Server by MUSIC API</pre>"
      ],
      "text/plain": [
       "<xarray.Dataset>\n",
       "Dimensions:                  (lat: 361, level: 1, lon: 720, time: 1)\n",
       "Coordinates:\n",
       "  * time                     (time) datetime64[ns] 2020-04-19\n",
       "  * level                    (level) int64 500\n",
       "  * lat                      (lat) float64 90.0 89.5 89.0 ... -89.0 -89.5 -90.0\n",
       "  * lon                      (lon) float64 0.0 0.5 1.0 1.5 ... 358.5 359.0 359.5\n",
       "    forecast_reference_time  datetime64[ns] 2020-04-19\n",
       "    forecast_period          (time) float64 0.0\n",
       "Data variables:\n",
       "    geopotential_height      (time, level, lat, lon) float32 5.14e+03 ... 4.8...\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    Origin:       CIMISS Server by MUSIC API"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set retrieve parameters\n",
    "data_code = \"NAFP_CRA40_FTM_6HOR_ANA\"\n",
    "init_time = \"2020041900\"\n",
    "valid_time = 0\n",
    "fcst_ele = \"GPH\"    # 位势高度场\n",
    "fcst_level = 500    # 500hPa层次\n",
    "level_type = \"-\"\n",
    "\n",
    "# retrieve data from CMADaSS\n",
    "data =  cmadaas_model_grid(data_code, init_time, valid_time, fcst_ele, fcst_level, level_type,\n",
    "                           varname='geopotential_height', units='gpm', \n",
    "                           levattrs={'long_name':'isobaric', 'units':'hPa', '_CoordinateAxisType':'isobaric'},\n",
    "                           cache=False)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
